package org.deltaset.meta.faces;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.ServletContext;
import org.deltaset.faces.FacesUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author dmakariev
 */
public class ReguestPhaseListener implements PhaseListener {

    private MetaCrudSession getMetaCrudSession() {
        final ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        final WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        final MetaCrudSession metaCrudSession = wac.getBean(MetaCrudSession.class);
        return metaCrudSession;
    }

    @Override
    public void beforePhase(PhaseEvent event) {
        //if (PhaseId.RESTORE_VIEW.equals(event.getPhaseId())) {
        System.out.println("\n\n!!!!!!!! beforePhase " + event.getPhaseId() + " " + this);
        //}
    }

    @Override
    public void afterPhase(PhaseEvent event) {
        if (PhaseId.RESTORE_VIEW.equals(event.getPhaseId())) {
            final boolean goBack = FacesUtils.getParameterBoolean("deltaset.meta.previousView");
            if (goBack) {
                getMetaCrudSession().previous();
            }
        }

        System.out.println(" !!!! afterPhase " + event.getPhaseId() + " this=" + this);
        if (PhaseId.PROCESS_VALIDATIONS.equals(event.getPhaseId())) {
            //weird issue during development - when using jetty:run , the phaseListener is registered twice and executed twice.    
            //the issue is related with the build system, and the usage of war overlays
            /////////                                 
            if (false == FacesUtils.getHasValidationErrors()) {
                final boolean goBack = FacesUtils.getParameterBoolean("deltaset.meta.previousView");
                if (goBack) {
                    // getMetaCrudSession().previous();
                } else {

                    final String entityClassName = FacesUtils.getParameterString("deltaset.meta.entityClassName");
                    final String viewName = FacesUtils.getParameterString("deltaset.meta.viewName");
                    final String recordId = FacesUtils.getParameterString("deltaset.meta.recordId");

                    final AttributeContext requestAttributeContext = new AttributeContext(entityClassName, viewName, recordId);
                    getMetaCrudSession().pushAttributeContext(requestAttributeContext);
                }
            }
            System.out.println("$$$$$$$$$$$ afterPhase " + event.getPhaseId());
            System.out.println("$$$$$$$$$$$ getMetaCrudSession().getAttributeContext()=" + getMetaCrudSession().getAttributeContext());
            if (null != getMetaCrudSession().getMetaEntryState()) {
                System.out.println("$$$$$$$$$$$ getMetaCrudSession().getMetaEntryState().metaEntry.entityClassName=" + getMetaCrudSession().getMetaEntryState().getMetaEntry().getEntityClass().getName());
            }
        }
    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.ANY_PHASE;
    }
}
